#include "ppp4g.h"
#include "debug.h"

#define CHAT_PEER_LIANTONG			"/tmp/peer_liantong"
#define CHAT_PEER_ETC_LIANTONG		"/etc/ppp/peer/liantong"
#define CHAT_PEER_VAR_LIANTONG		"/var/run/brg/ppp/peer/liantong"

#define CHAT_LIANTONG		"/tmp/liantong"
#define CHAT_ETC_LIANTONG	"/etc/ppp/chat/liantong"
#define CHAT_VAR_LIANTONG	"/var/run/brg/ppp/chat/liantong"

#define CHAP_SECRETS		"/tmp/chap-secrets"
#define CHAP_ETC_SECRETS	"/etc/ppp/chap-secrets"
#define CHAP_VAR_SECRETS	"/var/run/brg/ppp/chap-secrets"

#define ETC_WVDIAL_CONF		"/etc/wvdial.conf"
#define TMP_WVDIAL_CONF		"/tmp/wvdial.conf"



bool CreatePeersFile_Unicom(void)
{
	FILE *fs = NULL ;
	char buf[256] ;

	//printf("Create chat file ...\n");

	memset(buf, 0, sizeof(buf));
	sprintf(buf, "rm -rf %s", CHAT_PEER_LIANTONG);
	system(buf);
	
	fs = fopen(CHAT_PEER_LIANTONG, "ab+");
	if(fs== NULL)
		return false ;

	fputs("ttyUSB0\n", fs);
	fputs("115200\n", fs);
	fputs("connect '/usr/sbin/chat -v -f /etc/ppp/chat/liantong'\n", fs);
	fputs("debug\n", fs);
	fputs("noipdefault\n", fs);
	fputs("usepeerdns\n", fs);
	fputs("ipcp-accept-local\n", fs);
	fputs("ipcp-accept-remote\n", fs);
	fputs("user card\n", fs);
	fputs("unit 5001\n", fs);

	fclose(fs) ;

	memset(buf, 0, sizeof(buf));
	sprintf(buf, "cp %s %s", CHAT_PEER_LIANTONG, CHAT_PEER_ETC_LIANTONG);
	system(buf);

	memset(buf, 0, sizeof(buf));
	sprintf(buf, "cp %s %s", CHAT_PEER_LIANTONG, CHAT_PEER_VAR_LIANTONG);
	system(buf);

	return true ;
}


//Atention, For 4G, unicom and chinamobile can use a same ppp file.
bool CreateChatFile_Unicom(char *apn)
{
	FILE *fs = NULL ;
	char buf[256] ;

	//printf("Create chat file ...\n");

	memset(buf, 0, sizeof(buf));
	sprintf(buf, "rm -rf %s", CHAT_LIANTONG);
	system(buf);
	
	fs = fopen(CHAT_LIANTONG, "ab+");
	if(fs== NULL)
		return false ;

	fputs("ABORT \"NO CARRIER\"\n", fs);
	fputs("ABORT \"NO DIALTONE\"\n", fs);
	fputs("ABORT \"ERROR\"\n", fs);
	fputs("ABORT \"NO ANSWER\"\n", fs);
	fputs("ABORT \"BUSY\"\n", fs);
	fputs("TIMEOUT 10\n", fs);
	fputs("\"\" at\n", fs);
	memset(buf, 0, sizeof(buf));
	sprintf(buf, "OK AT+CGDCONT=1,\"IP\",\"%s\",,0,0 \n", apn);
	fputs(buf, fs);
	fputs("OK atdt*99#\n", fs);
	fputs("CONNECT\n", fs);

	fclose(fs) ;

	memset(buf, 0, sizeof(buf));
	sprintf(buf, "cp %s %s", CHAT_LIANTONG, CHAT_ETC_LIANTONG);
	system(buf);

	memset(buf, 0, sizeof(buf));
	sprintf(buf, "cp %s %s", CHAT_LIANTONG, CHAT_VAR_LIANTONG);
	system(buf);
	
	return true ;
}

bool CreateSecreteFile_Unicom(char *username, char *passwd)
{
	FILE *fs = NULL ;
	char buf[256] ;

	//printf("Create secrete file ...\n");

	memset(buf, 0, sizeof(buf));
	sprintf(buf, "rm -rf %s", CHAP_SECRETS);
	system(buf);
	
	fs = fopen(CHAP_SECRETS, "ab+");
	if(fs== NULL)
		return false ;

	fputs("# Secrets for authentication using CHAP\n", fs);
	fputs("# client	server	secret			IP addresses\n", fs);
	memset(buf, 0, sizeof(buf));
	sprintf(buf, "%s\t\t*\t%s\t*\n", username, passwd);
	fputs(buf, fs);

	fclose(fs) ;

	memset(buf, 0, sizeof(buf));
	sprintf(buf, "cp %s %s", CHAP_SECRETS, CHAP_ETC_SECRETS);
	system(buf);
	
	memset(buf, 0, sizeof(buf));
	sprintf(buf, "cp %s %s", CHAP_SECRETS, CHAP_VAR_SECRETS);
	system(buf);

	return true ;
}

/* Create the wvdialconf , for wvdial excute. */
bool createWvdialConf(std::string phoneNo, std::string username, std::string password, std::string apn)
{
	char buf[256] ;
	
	memset(buf, 0, sizeof(buf));
	sprintf(buf, "rm -rf %s", TMP_WVDIAL_CONF);
	system(buf);

	std::ofstream wvconf(TMP_WVDIAL_CONF) ;
	if (!wvconf.is_open())
    {
        std::cout << "Error opening file " << TMP_WVDIAL_CONF << std::endl;
		return false;
    }

	wvconf << "[Dialer Defaults]\n";
	
	wvconf <<"Init1 = ATZ\n";
	wvconf <<"Init2 = ATQ0 V1 E1 S0=0 &C1 &D2 +FCLASS=0\n";
	wvconf <<"Init3 = AT+CGDCONT=1, \"IP\", \"" << apn <<"\"\n" ;
	wvconf <<"Modem Type = Analog Modem\n";
	wvconf <<"ISDN = 0\n";
	wvconf <<"New PPPD = yes\n";
	wvconf <<"Phone = " << phoneNo << "\n";
	wvconf <<"Modem = /dev/ttyUSB0\n";
	wvconf <<"Username = " << username << "\n";
	wvconf <<"Password = " << password << "\n";
	wvconf <<"Baud = 9600\n";

	wvconf.close();

	memset(buf, 0, sizeof(buf));
	sprintf(buf, "cp %s %s", TMP_WVDIAL_CONF, ETC_WVDIAL_CONF);
	system(buf);

	return true ;
}

/*
restart pppd
*/
void restartPPPDCmd()
{
    std::ostringstream cmd ;
    std::string str ;

	cmd << "pppd connect " ;
    cmd << "'chat -v  \"\" \"AT\" \"\" \"AT+CGDCONT=1,\"IP\",\""<< g4.apn4g <<"\"\" \"\" \"ATD"<<phoneNo<<" CONNECT\"' " ;
    cmd << "/dev/ttyUSB0 115200 nodetach debug noauth defaultroute usepeerdns  crtscts user "<< g4.username<< " password "<<g4.passwd<< " -pc -ac $*  persist logfd 6 &" ;

	str = cmd.str();
	LOG(LOG_INFO, "%s\n", str.c_str()) ;

	system("killall pppd");
	system(str.c_str());
}

